﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using CommonLibrary;

namespace MessageServer.logging
{
    internal class stat
    {
        internal String _name;
        internal DateTime _start;
        internal DateTime _end;
        internal TimeSpan _duration;

        internal stat(String name)
        {
            _name = name + ":start";
            _start = DateTime.Now;
        }

        internal stat checkpoint(String nm)
        {
            endnow();
            stat nxt = new stat(nm);
            nxt._start = _end;
            return nxt;
        }

        internal void endnow()
        {
            _end = DateTime.Now;
            _duration = _end.Subtract(_start);
        }

        internal String record()
        {
            long duration = (_duration.Ticks / (TimeSpan.TicksPerMillisecond / 1000));
            return _name + ":Duration:" + duration;
        }
    }

    public class PerfStat
    {
        IList _perfs;
        stat _laststat;

        public PerfStat()
        {
            _perfs = new ArrayList();
        }

        public void start(String nm)
        {
            stat s = new stat(nm);
            _laststat = s;
            _perfs.Add(s);
        }

        public void checkpoint(String nm)
        {
            _laststat = _laststat.checkpoint(nm);
            _perfs.Add(_laststat);
        }

        public void dumpperf()
        {
            _laststat.endnow();
            foreach (stat s in _perfs)
            {
                Console.WriteLine(s.record());
            }
        }

        public void dumpperf(Logging log)
        {
            _laststat.endnow();
            foreach (stat s in _perfs)
            {
                log.LogInfo(s.record());
            }
        }
    }
}
